package cubelabs.nexus.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.Contact;
import org.brickred.socialauth.SocialAuthManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cubelabs.nexus.entity.Category;
import cubelabs.nexus.entity.Course;
import cubelabs.nexus.entity.User;
import cubelabs.nexus.service.NexusService;

@Controller
public class DashBoardController {

	@Autowired
	private NexusService nexusService;

	@RequestMapping(value = "/categories")
	public @ResponseBody List<Category> getAllCategories(final HttpServletRequest request)
			throws Exception {
		return nexusService.findAllCategories();
	}
	
	@RequestMapping(value = "/courses")
	public @ResponseBody List<Course> getAllCourses(final HttpServletRequest request, @RequestParam("categoryId") String category)
			throws Exception {
		
		Long categoryId = null;
		
		if(category != null && !category.equals("undefined")){
			categoryId = Long.parseLong(category);
		}
		return nexusService.findAllCoursesByCategoryId(categoryId);
	}
	
	@RequestMapping(value="/newcourse")
	public ModelAndView createCourse(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView("createCourse");
		Course course = new Course();
		modelAndView.addObject("command",course);
		return modelAndView;
		
	}
	
	@RequestMapping(value = "/course")
	public @ResponseBody List<Course> getCourse(final HttpServletRequest request, @RequestParam("id") String category)
			throws Exception {
		
		Long categoryId = null;
		
		if(category != null && !category.equals("undefined")){
			categoryId = Long.parseLong(category);
		}
		return nexusService.findAllCoursesByCategoryId(categoryId);
	}
	
	@RequestMapping(value = "/dashboard")
	public ModelAndView getRedirectURL(final HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("dashboard");
		mv.addObject("categories", nexusService.findAllCategories());
		return mv;
	}
	
	
}
